package com.yutel.gps.http;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ReportClient {
	private final Logger log = LoggerFactory.getLogger(ReportClient.class);
	private static ReportClient instence;
	private static CloseableHttpClient httpClient;
	private RequestConfig config;

	private ReportClient() {
		httpClient = HttpClients.createDefault();
		config = RequestConfig.custom().setSocketTimeout(1000).setConnectTimeout(1000).setConnectionRequestTimeout(1000)
				.build();
	}

	public static ReportClient getIns() {
		if (instence == null) {
			instence = new ReportClient();
		}
		return instence;
	}

	public String get(String url) {
		HttpGet httpGet = null;
		CloseableHttpResponse response = null;
		try {
			httpGet = new HttpGet(url);
			HttpContext context = HttpClientContext.create();
			// 执行发送
			httpGet.setConfig(config);
			response = httpClient.execute(httpGet, context);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				return EntityUtils.toString(entity);
			}
			return null;
		} catch (ClientProtocolException ex) {
			log.error("", ex);
			return null;
		} catch (IOException ex) {
			log.error("", ex);
			return null;
		} catch (Exception ex) {
			log.error("", ex);
			return null;
		} finally {
			try {
				if (response != null)
					response.close();
			} catch (IOException e) {
			}
		}
	}
}
